<template>
	<base-page title="佣金协议" backgroundColor="white">
		<view class="bg-secondary-warning px-16 py-8 fs-12 text-color-hint">为保证您的资金安全及佣金正常提现，需要您签署佣金服务协议。</view>
		<view class="mt-40 px-16 fs-14 text-color-regular leading-17">
			<view class="">签约短信已发送至您<text class="color-secondary-info">{{withdrawStore.signUrl}}</text>手机号上</view>
			<view class="mt-8">请注意查收【汇优财】名称的短信</view>
		</view>
		<view class="flex-col center w-full mt-40 px-16">
			<!--
			<image style="width:92px;height:100px;" src="@/pagesB/static/img-sign.png"></image>
			<view class="text-color-regular fs-16 font-semibold leading-22 mt-28">协议签约</view>
			<view class="fs-12 text-color-hint text-center leading-17 mt-8">为保证您的资金安全及佣金收入的合规性，请您阅读并签署佣金代发协议</view>
			<view class="radius-8 bg-primary h-46 w-full flex center text-white fs-16 font-normal mt-28" @click="toSign">去签约</view>
			-->
			<view class="mt-30 leading-17 text-color-hint fs-12">若遇签约或其他问题请 <button class="inline-flex transparent m-0 p-0 leading-17 fs-12 color-secondary-info" open-type="contact">联系客服</button>。</view>
		</view>
	</base-page>
</template>

<script setup name="Sign">
	import { onLoad, onShow, onHide } from '@dcloudio/uni-app'
	import { getCurrentInstance, ref } from 'vue'
	import { useWithdrawStore } from '@/stores/modules/withdraw.js'
	
	const instance = getCurrentInstance()
	const withdrawStore = useWithdrawStore()
	
	const needRefresh = ref(false)
	/**
	 * 点击签约，进入签约链接，签约完成返回小程序，查询签约状态
	 * 签约成功，返回提现页面，toast“银行卡绑定成功”
	 * 签约失败，返回签约页面，toast“签约失败”
	 */
	const toSign = () => {
		uni.navigateTo({
			url: `/pagesC/webview/webview?url=${encodeURIComponent(withdrawStore.signUrl)}&title=${encodeURIComponent('签约')}`,
			success(res) {
				console.log('打开签约地址', res)
				needRefresh.value = true
			},
		})
	}
	
	onShow(async () => {
		if (needRefresh.value) {
			try{
				uni.showLoading()
				await withdrawStore.getAgentEnterState()
				uni.hideLoading()
				needRefresh.value = false
				if (withdrawStore.agentEnterState === 4) {
					const eventChannel = instance.ctx.getOpenerEventChannel()
					eventChannel.emit('onSignResult', { result: true })
					uni.navigateBack()
					uni.showToast({
						title: '银行卡绑定成功',
						icon: 'none'
					})
				} else if (withdrawStore.agentEnterState === 5) {
					const eventChannel = instance.ctx.getOpenerEventChannel()
					eventChannel.emit('onSignResult', { result: false })
					uni.navigateBack()
					uni.showToast({
						title: '签约失败',
						icon: 'none'
					})
				}
			}catch(e){
				uni.hideLoading()
				needRefresh.value = false
			}
		}
	})
</script>
